package com.konne.nightmare.FastPublicOpinion.ui.information.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.collection.ArraySet;

import com.blankj.utilcode.util.LogUtils;
import com.coorchice.library.SuperTextView;
import com.konne.nightmare.FastPublicOpinion.Event;
import com.konne.nightmare.FastPublicOpinion.R;
import com.konne.nightmare.FastPublicOpinion.base.BaseMvpFragment;
import com.konne.nightmare.FastPublicOpinion.bean.MembersBean;
import com.konne.nightmare.FastPublicOpinion.mvp.presenter.MoveStudentPresenter;
import com.konne.nightmare.FastPublicOpinion.mvp.view.IMoveStudentView;
import com.konne.nightmare.FastPublicOpinion.ui.information.activity.GroupManagerActivity;
import com.konne.nightmare.FastPublicOpinion.utils.GlideUtils;
import com.konne.nightmare.FastPublicOpinion.utils.LoadProgressDialog;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.List;
import java.util.Set;

/**
 * Create by ws.
 * on Data:2022/1/25  Time:下午 8:00.
 * description: ** .
 */
public class MoveStudentFragment extends BaseMvpFragment<IMoveStudentView, MoveStudentPresenter> implements IMoveStudentView {
    public static MoveStudentFragment getInstance() {
        return new MoveStudentFragment();
    }

    @Override
    protected MoveStudentPresenter createPresenter() {
        return new MoveStudentPresenter();
    }

    @Override
    public LoadProgressDialog getLoadDialog() {
        return null;
    }

    @Override
    public void cancelLoadDialog() {

    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
    @Subscribe
    public void onEvent(Event.CloseMoveStudentView event){
        getActivity().finish();
    }
    @Override
    public int getLayout() {
        return R.layout.frag_move_student;
    }

    @Override
    public void initView() {
    findViewById(R.id.stv_submit).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            GroupManagerActivity.start(1);
        }
    });
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        mPresenter.init();
    }

    @Override
    public void updataTeamData(List<MoveStudentPresenter.StudentTeam> teams) {
        ExpandableListView listView = findViewById(R.id.elv_content);
        listView.setGroupIndicator(null);

        listView.setAdapter(new MoveAdapter(teams));
        try {
            for (int i = 0; i < teams.size(); i++) {//展开所有
                listView.expandGroup(i);
            }
        } catch (Exception e) {
            LogUtils.i(e);
        }


    }

    /**
     * 拿数据的时候开源把这个Adapter拿出去，便于管理，set为选择的数据，开源通过interface回调拿数据，getGroupView，getChildView可以加入ViewHolder优化
     * */
    class MoveAdapter extends BaseExpandableListAdapter {
        private List<MoveStudentPresenter.StudentTeam> teams;
        private Set<MembersBean> set=new ArraySet<>();
        public MoveAdapter(List<MoveStudentPresenter.StudentTeam> teams) {
            this.teams = teams;
        }

        @Override
        public int getGroupCount() {
            return teams.size();
        }

        @Override
        public int getChildrenCount(int i) {
            return teams.get(i).datas.size();
        }

        @Override
        public Object getGroup(int i) {
            return teams.get(i);
        }

        @Override
        public Object getChild(int i, int i1) {
            return teams.get(i).datas.get(i1);
        }

        @Override
        public long getGroupId(int i) {
            return i;
        }

        @Override
        public long getChildId(int i, int i1) {
            return i + i1;
        }

        @Override
        public boolean hasStableIds() {
            return true;
        }

        @Override
        public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
            if (view == null) {
                view = LayoutInflater.from(getContext()).inflate(R.layout.item_cgm_group, null);
            }
            MoveStudentPresenter.StudentTeam studentTeam = (MoveStudentPresenter.StudentTeam) getGroup(i);
            TextView tv_title = view.findViewById(R.id.tv_title);
            tv_title.setText(studentTeam.title);
            SuperTextView stv_sub_title = view.findViewById(R.id.stv_sub_title);
            stv_sub_title.setText(String.format("%d 人", studentTeam.datas.size()));
            stv_sub_title.setDrawableRotate(b ? -90 : 90);
            return view;
        }
        //
        @Override
        public View getChildView(int i, int i1, boolean isExpanded, View view, ViewGroup viewGroup) {
            if (view == null) {
                view = LayoutInflater.from(getContext()).inflate(R.layout.item_cgm_student, null);
            }
            MembersBean data = (MembersBean) getChild(i, i1);
            ImageView ic_sel = view.findViewById(R.id.ic_sel);
            ImageView iv_avatar = view.findViewById(R.id.iv_avatar);
            GlideUtils.loadCircleImage(data.getAvatar(), iv_avatar);
            TextView name = view.findViewById(R.id.tv_name);
            name.setText(data.getName());
            ic_sel.setImageResource(set.contains(data)?R.drawable.login_xz:R.drawable.login_wxz);

            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(set.contains(data)){
                        set.remove(data);
                    }else {
                        set.add(data);
                    }
                    ic_sel.setImageResource(set.contains(data)?R.drawable.login_xz:R.drawable.login_wxz);
                }
            });
            return view;
        }

        @Override
        public boolean isChildSelectable(int i, int i1) {
            return true;
        }
    }

}
